package lesson02;


import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;

//目的：实现鼠标画画,测试鼠标监听
public class MyMouseListener {
    public static void main(String[] args) {
   new MyFrame("画图");
    }
}

class MyFrame extends Frame {
    //画画需要画笔，需要监听鼠标当前的位置，需要集合来存储这个点
    ArrayList points;
    public MyFrame(String title){
        super(title);
        //存鼠标点击的点
        points=new ArrayList<>();

        setBounds(200,200,200,200);
        setVisible(true);
       //需要鼠标监听器
        this.addMouseListener(new MyMouseListener());
    }

    //画画，监听鼠标的事件
    @Override
    public void paint(Graphics g) {

        Iterator iterator=points.iterator();
        while(iterator.hasNext()){
            Point point=(Point) iterator.next();
            g.setColor(Color.BLUE);
            g.fillOval(point.x,point.y,10,10);
        }

    }
    public void addPaint(Point point){
        points.add(point);
    }
   //适配器模式
    private  class MyMouseListener extends MouseAdapter {
       @Override
       //鼠标：按下，弹起，按住不放
       public void mouseClicked(MouseEvent e) {
          MyFrame myframe=(MyFrame) e.getSource() ;
          //这个我们点击的时候，就会在界面上产生一个点！画
          //这个点就是鼠标的点
           myframe.addPaint(new Point(e.getX(),e.getY()));
       //每次点击鼠标都不要重新画一遍
           myframe.repaint();
       }

    }

}